/* $Id: Scene.java 32 2006-09-25 21:54:18Z funsheep $ */
package net.sourceforge.ftgl.demos.showroom.scene;

import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;

import net.java.games.jogl.GL;
import net.java.games.jogl.GLU;
import net.sourceforge.ftgl.demos.showroom.Trackball;
import net.sourceforge.ftgl.glfont.FTFont;
import net.sourceforge.ftgl.glfont.FTGLBitmapFont;
import net.sourceforge.ftgl.glfont.FTGLExtrdFont;
import net.sourceforge.ftgl.glfont.FTGLOutlineFont;
import net.sourceforge.ftgl.glfont.FTGLPixmapFont;
import net.sourceforge.ftgl.glfont.FTGLPolygonFont;
import net.sourceforge.ftgl.glfont.FTGLTextureFont;

/**
 * @author funsheep
 *
 */
public class Scene
{

	
	public static final int EDITING = 1;
	public static final int INTERACTIVE = 2;

	public static final int FTGL_BITMAP = 0;
	public static final int FTGL_PIXMAP = 1;

	public static final int FTGL_OUTLINE = 2;
	public static final int FTGL_POLYGON = 3;
	public static final int FTGL_EXTRUDE = 4;
	public static final int FTGL_TEXTURE = 5;


	public static final float [][] COLORS = new float[][]
	{
		{ 0.0f, 0.0f, 0.0f, 1.0f},
		{ 1.0f, 0.0f, 0.0f, 1.0f},
		{ 0.0f, 1.0f, 0.0f, 1.0f},
		{ 0.0f, 0.0f, 1.0f, 1.0f},
		{ 1.0f, 1.0f, 0.0f, 1.0f},
		{ 0.0f, 1.0f, 1.0f, 1.0f},
		{ 1.0f, 0.0f, 1.0f, 1.0f},
		{ 1.0f, 1.0f, 1.0f, 1.0f},
	};

	private GL gl;
	private GLU glu;

	/**
	 * The 6 different types of supported fonts.
	 * <p>Note: previously static FTFont* fonts[6];
	 */
	private final FTFont[] fonts = new FTFont[6];
	public FTFont infoFont = null;
	public FTFont ascenderfont;

	public int current_ftfont = FTGL_EXTRUDE;
	public int current_color = COLORS.length-1;

	private String current_awtfont;
	private final FontRenderContext context;


	public int   mode = EDITING;
	public float zoom = 2.0f;
	public Trackball tb = new Trackball();

	public String displayString = "jftgl";
	public int carat = this.displayString.length();

	
	public Scene(String font)
	{
		this(font, FTFont.STANDARDCONTEXT);
	}

	public Scene(String font, FontRenderContext context)
	{
		this.tb.tbInit(MouseEvent.BUTTON1);
		this.context = context;
		this.setUpFonts(font);
	}

	
	/**
	 * Create fonts.
	 */
	public void setUpFonts(String awtFont)
	{
		this.current_awtfont = awtFont;
		Font font = Font.decode(this.current_awtfont).deriveFont(100f);
		this.fonts[FTGL_BITMAP]  = new FTGLBitmapFont(font, context);
		this.fonts[FTGL_PIXMAP]  = new FTGLPixmapFont(font, context);
		this.fonts[FTGL_OUTLINE] = new FTGLOutlineFont(font, context);
		this.fonts[FTGL_POLYGON] = new FTGLPolygonFont(font, context);
		this.fonts[FTGL_EXTRUDE] = new FTGLExtrdFont(font, context);
		this.fonts[FTGL_TEXTURE] = new FTGLTextureFont(font, context);

		((FTGLExtrdFont)this.fonts[FTGL_EXTRUDE]).setDepth(20f);

//		for (int i=0;i<this.fonts.length;i++)
//		{
//			// TODO: Fix implementation to support this :D
//			// this.fonts[i].faceSize(144f);
//		}

		this.infoFont = new FTGLPixmapFont(font);
		this.infoFont.faceSize(18);
		this.ascenderfont  = new FTGLPolygonFont(font.deriveFont(20f));
		this.setGLGLUOnFTFonts();
	}

	
	public FTFont setCurrentFTFont(int index)
	{
		this.current_ftfont = index;
		if (this.current_ftfont >= this.fonts.length)
			this.current_ftfont = 0;
		if (this.current_ftfont < 0)
			this.current_ftfont = this.fonts.length-1;
		return this.fonts[this.current_ftfont];
	}
	
	public float [] setCurrentColor(int index)
	{
		this.current_color = index;
		if (this.current_color >= COLORS.length)
			this.current_color = 0;
		if (this.current_color < 0)
			this.current_color = COLORS.length-1;
		return COLORS[this.current_color];
	}
	
	public FTFont getCurrentFTFont()
	{
		return this.fonts[this.current_ftfont];
	}

	public void setGLGLU(GL gl, GLU glu)
	{
		this.gl = gl;
		this.glu = glu;
		this.setGLGLUOnFTFonts();
	}

	/**
	 * This method sets the new GL context, if it has changed.
	 * When overriding this method, super.setGL(GL gl); must be called!
	 * @param gl The new GL context.
	 * @param glu The new GLU context.
	 */
	public void setGLGLUOnFTFonts()
	{
		for (int i = 0; i < this.fonts.length; i++)
		{
			this.fonts[i].setGLGLU(gl, glu);
			this.fonts[i].clearCache(true);
		}
		this.infoFont.setGLGLU(gl, glu);
		this.ascenderfont.setGLGLU(gl, glu);
	}

}
